import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ProfileServiceProxy, DataItemServiceProxy, DataItemDetailViewDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { IOrganizationUnitsTreeComponentData, OrganizationUnitsTreeComponent } from '../../shared/organization-unit-tree.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { SelectItem } from 'primeng/api';

@Component({
  selector: 'DataItemDetailCreate',
  templateUrl: './DataItemDetailCreate.component.html',
  styleUrls: ['./DataItemDetailCreate.component.css']
})

export class DataItemDetailCreateComponent extends AppComponentBase {
  @ViewChild('createOrEditModal') modal: ModalDirective;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;
  itemAyy = [];
  selectedItem: string;
  ItemDetailView: DataItemDetailViewDto = new DataItemDetailViewDto();
  constructor(injector: Injector,
    private _dataItemServiceProxy: DataItemServiceProxy,
    private _profileService: ProfileServiceProxy) {
    super(injector);
  }
  show(ItemDetailId?: string, selectedItem?: string): void {
    console.log(ItemDetailId);
    console.log(selectedItem);
    this.selectedItem = selectedItem;
    if (!ItemDetailId) {
      this.active = true;
    }
    if (ItemDetailId === undefined) {
      this.modal.show();
    } else {
      this._dataItemServiceProxy.getDataItemDetailForEdit(ItemDetailId).subscribe(resultdata => {
        this.ItemDetailView = resultdata;
        console.log(resultdata);
        if (ItemDetailId) {
          this.active = true;
        }
        this.modal.show();
      });
    }
    // this.active = true;
    // this.modal.show();
  }
  onShown(): void {
    this._dataItemServiceProxy.getDataItemAllList().subscribe(resultdata => {
      this.itemAyy = resultdata;
      if (!this.selectedItem) {
        this.selectedItem = this.itemAyy[0].value;
      }
    });
  }

  save(): void {
    console.log(this.selectedItem);
    this.ItemDetailView.itemId = this.selectedItem;
    this.saving = true;
    if (!!this.ItemDetailView.id) {
      this._dataItemServiceProxy.updateDataItemDetail(this.ItemDetailView)
        .pipe(finalize(() => { this.saving = false; }))
        .subscribe(() => {
          this.notify.info(this.l('SavedSuccessfully'));
          this.close();
          this.modalSave.emit(null);
        });
    } else {
      // let input = new DataItemDetailModel();
      // input.itemId = this.ItemDetailView.itemId;
      // input.itemName = this.ItemDetailView.itemName;
      // input.itemValue = this.ItemDetailView.itemValue;
      // input.itemCode = this.ItemDetailView.itemCode;
      // input.description = this.ItemDetailView.description;
      this.ItemDetailView.id = undefined;
      console.log(this.ItemDetailView);
      this._dataItemServiceProxy.createDataItemDetail(this.ItemDetailView)
        .pipe(finalize(() => { this.saving = false; }))
        .subscribe(() => {
          this.notify.info(this.l('SavedSuccessfully'));
          this.close();
          this.modalSave.emit(null);
        });
    }
  }

  close(): void {
    this.ItemDetailView = new DataItemDetailViewDto();
    this.active = false;
    this.modal.hide();
  }
}

